package app

import "net/http"

type DictLite struct {
	Label  string `json:"label"`
	Value interface{} `json:"value"`
	Children interface{} `json:"children,omitempty"`
}

var dictMap = make(map[string]*DictLite)

func RegDict(key string,dict *DictLite)  {
	dictMap[key] = dict
}

func GetDict(key string) (d *DictLite) {
	d,ok := dictMap[key]
	if !ok {
		d = &DictLite{}
		dictMap[key] = d
	}
	return d
}

func ServeDict(w http.ResponseWriter,r *http.Request)  {
	key := r.URL.Query().Get("key")
	v := dictMap[key]
	if v != nil && v.Value == nil {
		RenderJSON(w,v.Children,nil)
	} else {
		RenderJSON(w,v,nil)
	}
}
